<?php

namespace App\Models;

use App\Models\CommonModel as Model;

/**
 * 配置表
 * Class AppConfigModel
 * @package App\Models
 */
class AppConfigModel extends Model
{
    const HOT_TICKET = 'hot_coupons';
    const KEYWORDS = 'keywords';

    public $table = 'app_config';
    public $fillable = [
        'key',
        'value',
    ];

    public $searchable = [
        'key',
        'value'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'key' => 'required|string',
        'value' => 'required|string',
    ];

    /**
     * @param $key
     * @return string
     */
    public static function findOneByKey($key)
    {
        $model = self::where('key', $key)->where('is_enabled', 1)->first();

        if (empty($model)) {
            return '';
        } else {
            return (string) $model->value;
        }
    }

    /**
     * @param $key
     * @return string
     */
    public static function updateOneByKey($key, $value)
    {
        $ret = self::where('key', $key)->where('is_enabled', 1)->update(['value' => $value]);

        return $ret;
    }
}
